package com.witmed.user.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.witmed.user.intent.UserIntent
import com.witmed.user.repos.RegisterRepository
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import okhttp3.RequestBody

sealed class RegisterState{
    data class RegisterSuccess(var code:Int):RegisterState()
    data class Error(val msg:String):RegisterState()
    object Loading:RegisterState()
//    发送验证码成功
    data class RegCodeSuccess(var code:String):RegisterState()
    //验证码注册成功
    data class RegisterCodeSuccess(var code:Int):RegisterState()
}


class RegisterViewModel:ViewModel(){

    val registerRepo by lazy { RegisterRepository() }
    //创建一个意图管道
    val registerChannel= Channel<UserIntent>(Channel.UNLIMITED)
    //可变状态的数据流
    private val _registerState= MutableStateFlow<RegisterState>(RegisterState.Loading)
    //可观察的数据流
    val registerState:StateFlow<RegisterState>
        get() = _registerState

    init {
        handlerIntent()
    }

    //接收意图，并执行操作
    private fun handlerIntent() {
        //开启协程
        viewModelScope.launch {
            //接收到view层发送的意图流
            registerChannel.receiveAsFlow().collect{
                //判断是什么意图
                when(it){
                    is UserIntent.register->
                        //调用注册
                       register(it.username,it.password)
                    is UserIntent.sendRegCode->
                        sendRegCode(it.phone)
                    is UserIntent.registerCode->
                        registerCode(it.body)
                    else->{}
                }
            }
        }

    }

    /**
     * 验证码注册
     */
    private fun registerCode(body: RequestBody) {
        viewModelScope.launch {
            //请求注册
            val resp=registerRepo.registerCode(body)

            if(resp.code==0){
//                返回成功状态，并携带需要的数据
                _registerState.value=RegisterState.RegisterCodeSuccess(resp.code)
            }else{
//                返回失败状态，并携带失败的原因
                _registerState.value=RegisterState.Error(resp.msg)
            }
        }
    }

    //发送验证码
    fun sendRegCode(phone:String){
        viewModelScope.launch {
            //请求注册
            val resp=registerRepo.sendRegCode(phone)

            if(resp.code==0){
//                返回成功状态，并携带需要的数据
                _registerState.value=RegisterState.RegCodeSuccess(resp.data!!)
            }else{
//                返回失败状态，并携带失败的原因
                _registerState.value=RegisterState.Error(resp.msg)
            }
        }
    }

    //注册
    fun register(username:String,password:String){
        viewModelScope.launch {
            //请求注册
            val resp=registerRepo.register(username, password)

            if(resp.code==0){
//                返回成功状态，并携带需要的数据
                _registerState.value=RegisterState.RegisterSuccess(resp.code)
            }else{
//                返回失败状态，并携带失败的原因
                _registerState.value=RegisterState.Error(resp.msg)
            }
        }
    }
}